<?
/////////////////////////////////////////////////////////////////////////////////////////////////	
function get_conn(){
	global $db;


		$server = "10.6.171.5";
		$user ="drsa_user";
		$pwd = "Pr1nc3s4";
		$dbase="drsa_user";	
		
			
	if($_SERVER['SERVER_NAME'] =="valinor.Belkin"){
		$server = "localhost";
		$user ="root";
		$pwd = "";
		$dbase="drsa";		
	}
	
	
	if($_SERVER['SERVER_NAME'] =="chequeame.org"){
		$server = "localhost";
		$user ="checkorg_wuser";
		$pwd = "wuser";
		$dbase="checkorg_franco";		
	}	
	
	if($db ==''){
		@ $db=mysql_connect ($server, $user, $pwd);				
		if (!$db){		   
			error_handling("get_conn: ".$sql,mysql_error());			
		} 								
		mysql_select_db ($dbase); 
	}	
	return $db;
}		
//////////////////////////////////////////////////////////////////////////////////////////////////	
function getStatus($value,$param){
	if ($value == $param)
		return "selected";
	else	
		return "";
}
//////////////////////////////////////////////////////////////////////////////////////////////////	
function dbcombo($value,$descripcion,$tabla,$nombre,$seleccionado,$order,$javascript,$extraitems){

		$sql= "select ".$value." as myvalue , ".$descripcion." as mydesc from ".$tabla." order by ".$order;
		
		$result = mysql_query($sql);
		$combo= " <select name='".$nombre."' ".$javascript." class='contenido_forma'>";
		if($result){
			if(mysql_num_rows($result)>0){
				$data = $extraitems;
				$num_results = mysql_num_rows($result);
				  for ($i=0; $i <$num_results; $i++){		 
					$row = mysql_fetch_array($result);
					$selected="";
					$selected = getStatus($row["myvalue"],$seleccionado);										
					$data.= " <option value='".$row["myvalue"]."'".$selected." >".$row["mydesc"]."</option>";
				  }//for
			}else{
				$data.= " <option value='0'>Not Available</option>";
			}
		}//if
		else{
			error_handling("dbcombo: ".$sql,mysql_error());
		}
		$combo.= $data."</select>";
		 if($data==""){
		 	$combo = $sql;
		 }
		 return $combo;
}//function 
	
//////////////////////////////////////////////////////////////////////////////////////////////////	
/*
select id as myvalue, desc as mydesc from ....
*/
function dbcombosql($sqlquery,$nombre,$seleccionado,$javascript,$extraitems){
	$result = mysql_query($sqlquery);
	$combo= " <select name='".$nombre."'".$javascript." class='contenido_forma'> ";
	$data = $extraitems;
	if($result){
		$num_results = mysql_num_rows($result);
		if($num_results>0){
		  for ($i=0; $i <$num_results; $i++){		 
			$row = mysql_fetch_array($result);
			$selected="";
			$selected = getStatus($row["myvalue"],$seleccionado);										
			$data.= " <option value='".$row["myvalue"]."'".$selected." >".$row["mydesc"]."</option>";
		  }//for
		}else{
			$data.= " <option value='0'>Not Available&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>";
		}
	}else{
		error_handling("dbcombosql: ".$sqlquery,mysql_error());
	}
	$combo.= $data."</select>";
	 if($data==""){
		$combo = $sqlquery;
	 }
	 return $combo;
}//function 
//////////////////////////////////////////////////////////
function format_date($date){
	if($date =='0000-00-00' || $date == ""){
		return '';
	}
	$date_array = explode('-',$date);
	$new_date_a=$date_array[0];
	$new_date_m=$date_array[1];
	$new_date_d=$date_array[2];	
	
	if(abs($new_date_d)<10){
		$new_date_d = abs($new_date_d);
	}

	if(abs($new_date_m)<10){
		$new_date_m = abs($new_date_m);
	}	
	
	$new_date_a = substr($new_date_a,2,4);	
	
	$new_date = $new_date_m."/".$new_date_d."/".$new_date_a;
	return $new_date;
}

//////////////////////////////////////////////////////////
function un_format_date($date){
	$date_array = explode('/',$date);
	//07/27/2007
	$new_date_a= $date_array[2];
	$new_date_m=$date_array[0];
	$new_date_d=$date_array[1];	

	$new_date = $new_date_a."-".$new_date_m."-".$new_date_d;
	return $new_date;
}

//////////////////////////////////////////////////////////
function format($precio){
	$partes = explode(".",$precio);
	$precio = $partes[0];
	$decimales =$partes[1];
	
	if($precio =='0.00'){
		return "$0.00";
	}
	if($decimales ==''){
		$decimales="00";
	}
		
	if(strlen($precio)>6){
		$p3 = substr($precio,strlen($precio)-3,3);
		$p2 = substr($precio,strlen($precio)-6,3);
		$p1 = substr($precio,0,strlen($precio)-6);	
		$formated = "$".$p1.",".$p2.",".$p3;
	}
	else{
		if(strlen($precio)>3){
			$p3 = substr($precio,strlen($precio)-3,3);
			$p2 = substr($precio,0,strlen($precio)-3);	
			$formated = "$".$p2.",".$p3;	
		}else{
			if($precio != 0){
				$formated = "$".$precio;
			}else{
				$formated ="N/A";
			}	
		}
	}
	if($formated !='N/A'){
		$formated .=".".$decimales;
	}			
	
	return $formated;

}



////////////////////////////////////////////////////////////////////

function get_modules($cols,$id_rol){
	if($id_rol !=''){
		$sql ="select * from rol_modulo where id_rol=".$id_rol;
		$result = mysql_query($sql);
		if($result){
			while($row = mysql_fetch_array($result)){
				$modulos.=$row['id_modulo'].",";
			}
			$modulos = substr($modulos,0,(strlen($modulos)-1));
		}else{
			
			error_handling("get_modules: ".$sql,mysql_error());
		}
	}
	
	
	////////////////////////////////////////////////////////////////////
	$sql ="select name,link,nombre , id_modulo
			from 
			modulos m, categorias_menu cm
			where m.categoria_id = cm.categoria_id
			 order by nombre,name ";
	$result = mysql_query($sql);
	if($result){
	    $counter=0;  
		while($row = mysql_fetch_array($result)){		
		$counter ++;
					 if($counter % 2 ==0){
									$class_ = "sub";
								}else{
									$class_ = "sub2";
								}  
								
			if($cat!=$row['nombre']){
				$data.="<tr>
							<td align='center' class='cpbarratitulo2'> 
								".$row['nombre']."
							</td>
						</tr>";
				$cat=$row['nombre'];
				
			}
			$data.="<tr class='".$class_."'>
						<td> 
							<input type='checkbox' name='id_modulo[]' value='".$row['id_modulo']."' ".is_in_list($modulos,$row['id_modulo']).">
							<strong class='texto'>".$row['name']."</strong>    |    <a class='ac_vinculo1' href='".$row['link']."' target='_blank'> ".$row['link']."</a>
						</td>
					</tr>";
			
			
		}//while
		return "<table border='0'>".$data."</table>";
	}else{
		
		error_handling("get_modules: ".$sql,mysql_error());
	}
}

////////////////////////////////////////////////////////////////////

function get_roles($cols,$id_user,$filtro){
	if($id_user !=''){
		$sql ="select * from user_rol where id_user=".$id_user;
		$result = mysql_query($sql);
		if($result){
			while($row = mysql_fetch_array($result)){
				$roles.=$row['id_rol'].",";
			}
			$roles = substr($roles,0,(strlen($roles)-1));
		}else{
			
			error_handling("get_roles: ".$sql,mysql_error());
		}
	}
	
	
	////////////////////////////////////////////////////////////////////
	if($filtro !=''){
		$parts = explode(',',$filtro);
		if(count($parts)>1){
			for($i=0;$i<count($parts);$i++){
				$where .=" id_rol =".$parts[$i];
				if( ($i+1) != count($parts)){
					$where .=" or ";
				}
			}
			$where =" where ".$where;
			
		}else{
			$where =" where id_rol =".$filtro;
		}
	}

	
	$sql ="select * from rol ".$where;
	$result = mysql_query($sql);
	if($result){
		while($row = mysql_fetch_array($result)){
			if($counter ==0){
				$data.="<tr>";
			}
			$data.="<td><input type='checkbox' name='id_rol[]' value='".$row['id_rol']."' ".is_in_list($roles,$row['id_rol']).">".$row['name']."</td>";
			$counter++;
			if($counter ==$cols){
				$data.="</tr>";
				$counter =0;
			}
		}//while
		return "<table border='0' class='contenido_forma'>".$data."</table>";
	}else{
		
		error_handling("get_roles: ".$sql,mysql_error());
	}
}

////////////////////////////////////////////////
function get_my_roles($id_user){
	if($id_user !=''){
		$sql ="select * from user_rol where id_user=".$id_user;
		$result = mysql_query($sql);
		if($result){
			$counter =0;
			while($row = mysql_fetch_array($result)){
				$roles[$counter] .=$row['id_rol'];
				$counter++;
			}
			return $roles;
		}else{
			error_handling("get_my_roles: ".$sql,mysql_error());
		}
	}else{
		echo "get_my_roles "; 
	}
}

/////////////////////////////////////////
function is_admin($id_user){
	$sql = "select count(id_user) as existe from user_rol where (id_rol=1 or id_rol=13)  and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			return true;
		}else{
			return false;
		}
	}else{
		
		error_handling("is_admin: ".$sql,mysql_error());
	}
} 
/////////////////////////////////////////
function is_affiliate($id_user){
	$sql = "select count(id_user) as existe from user_rol where id_rol=2 and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			return true;
		}else{
			return false;
		}
	}else{
		
		error_handling("is_affiliate: ".$sql,mysql_error());
	}
} 
/////////////////////////////////////////
function is_affiliate_admin($id_user){
	$sql = "select count(id_user) as existe from user_rol where id_rol=12 and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			return true;
		}else{
			return false;
		}
	}else{
		error_handling("is_affiliate_admin: ".$sql,mysql_error());
	}
} 
/////////////////////////////////////////
function is_dev_team($id_user){
	$sql = "select count(id_user) as existe from user_rol where id_rol=3 and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			return true;
		}else{
			return false;
		}
	}else{
		error_handling("is_dev_team: ".$sql,mysql_error());
	}
} 

/////////////////////////////////////////
function is_house_agent($id_user){
	$sql = "select count(id_user) as existe from user_rol where id_rol=14 and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			return true;
		}else{
			return false;
		}
	}else{
		error_handling("is_house_agent: ".$sql,mysql_error());
	}
} 

/////////////////////////////////////////
function do_security($page){
	$url = $_SERVER['HTTP_REFERER'];
	$url = explode("/",$url);
	$url = $url[(count($url)-1)];

	$url = explode("?",$url);
	$REFERER = $url[0];

	$page= substr($page,1,(strlen($page)));
	
	if($REFERER =='menu.php'){
		$result = check_rights($page);
	}else{
		$result = check_rights_no_module();
	}
	
	if($result == 0){
		$kickout="<script>
					alert('Your session has expire or you are trying to access without do login');
					document.location.href='/';
				 </script>
				 <meta http-equiv='refresh' content='0;url=/'>
				 ";	
		 exit;
	}
}
/////////////////////////////////////////
function check_rights($page){
	if(!is_array($_SESSION['session_roles'])){
		return 0;
	}else{
	
		$roles =$_SESSION['session_roles'] ;
		
		for($i=0; $i < count($roles); $i++){
			$roles_list.=$roles[$i].",";
		}
		$roles_list = substr($roles_list,0,( strlen($roles_list) -1 ));
		
		$sql ="	select id_rol from rol_modulo rm, modulos m
				where m.link like'%".$page."%'  
				and id_rol in (".$roles_list.")
				and rm.id_modulo = m.id_modulo ";
		$result = mysql_query($sql);
		if($result){
			if(mysql_num_rows($result)== 0){
				return 0;		
			}else{
				return 1;
			}
		}else{
			error_handling("check_rights: ".$sql,mysql_error());
		}				
	}
}//check_rights

/////////////////////////////////////////
function check_rights_no_module(){
	if($_SESSION['session_roles']  ==''){
		return 0;
	}
	return 1;
}//check_rights
///////////////////////////////////////////
function is_checked($searched,$selecteds){
	$selecteds_array = explode(',',$selecteds);
	for($i=0;$i<count($selecteds_array);$i++){
		if($searched == $selecteds_array[$i]){
			return "checked";
		}
	}
}
///////////////////////////////////////////////////////
function add_bitacora($msg,$sql){
	$username = $_SESSION['session_username'];
	$ip = $_SERVER['REMOTE_ADDR'];
	$system_date =date('Y-m-d');
	$system_time =date('H:i:s');
	$id_user = $_SESSION['session_id_user'];
	if($id_user==''){
		$id_user = 999999;
	}
	$sql ="INSERT INTO bitacora (username,action,sql_exec,system_date,ip,id_user,system_time) VALUES 
			(\"".$username."\",\"".$msg."\",\"".removequotes($sql)."\",'".$system_date."','".$ip."',".$id_user.",'".$system_time."')";
	$result = mysql_query($sql);
	if(!$result){
		error_handling("add_bitacora: ".$sql,mysql_error());
	}
}
///////////////////////////////
function removequotes($sql){
	$new_sql;
	for($i=0;$i < strlen($sql);$i++){
		if(( $sql[$i]!="'") && ( $sql[$i]!="\"") && ( $sql[$i]!=",") && ( $sql[$i]!="\n"))  {
			$new_sql .= $sql[$i];
		}else{
			//$new_sql .= "";
		}
	}
	return $new_sql;
}
///////////////////////////////
function rq($text){
	$new_text;
	for($i=0;$i < strlen($text);$i++){
		if( $text[$i]=="\"")  {
			$new_text .= "'";
		}else{
			$new_text .= $text[$i];
		}
	}
	return $new_text;
}
///////////////////////////////////
function error_handling($sql,$error){
	$to = "dontpanic@danramza.com";
	$head = "Error report from ".$_SERVER['SERVER_NAME'];

	foreach($_REQUEST as $key=>$value){
     $variables .= $key." =".$value."<br>";
    }
	
	foreach($_SESSION as $key=>$value){
     $session_vars .= $key." =".$value."<br>";
    }	


	$body=  $error." <br><br>Executed:<br>".$sql." <br><br>at ".date("l dS of F Y h:i:s A").
	"<br><br>Error in File: <br>".$_SERVER['PHP_SELF'].
	"<br><br>Request Vars<br>".$variables.
	"<br><br>Session Vars<br><br>".$session_vars;


	$headers .= "From:".$_SERVER['SERVER_NAME']."<info@danramza.com>\n";
	$headers .= "X-Sender: <info@danramza.com>\n";
	$headers .= "X-Mailer: PHP\n"; //mailer
	$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
	$headers .= "Return-Path: <info@danramza.com>>\n";
	$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

	if($_SESSION['session_id_user']!=''){
		if($_SERVER['SERVER_NAME'] =="valinor.Belkin"){
			echo $body;		
		}else{
			$x =mail($to, $head , $body ,$headers);
			$script ="<script>document.location.href='../error.php'</script>"; 	  
			echo $script;
			
		}
	}


	
}


///////////////////////////////////////////////
function is_in_list($arts,$id_product){
	$arts_list= explode(',',$arts);
	for($i=0; $i < count($arts_list);$i++){
		if($arts_list[$i] == $id_product){
			return " checked ";
		}
	}
	return "";
}
///////////////////////////////////////////////
function is_in_list_array($arts_array,$id_product){
	for($i=0; $i < count($arts_array);$i++){
		if($arts_array[$i] == $id_product){
			return " checked ";
		}
	}
	return "";
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////FLOORREPORT FUNCTIONS//////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

function get_header(){
	$url= $_SERVER['REQUEST_URI'];
	$url_array = explode("/",$url);
	for($i=0;$i < (count($url_array) -2) ; $i++){
		$base.="../";
	}
	if($_SESSION['session_id_user'] !=''){
		$include = $base."resources/top_on.php";	
	}else{
		$include = $base."resources/top_off.php";
	}
	include($include);
}
////////////////////////////////////////////////////////////////////////////

function get_foother(){

	if($_SESSION['session_id_user'] !=''){
		$include = $base."resources/fot_on.php";	
	}else{
		$include = $base."resources/fot_off.php";
	}
	include($include);
}


/////////////////////////////////////////////////////////

function get_user_name($id){
	$sql = "select fname, lname from users where id_user=".$id;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['fname']." ".$row['lname'];
	}else{
		error_handling("get_client_name: ".$sql,mysql_error());
	}
}
/////////////////////////////////////////////////////////

function get_costumer_name($id_costumer){
	$sql = "select fname, lname from costumer where id_costumer=".$id_costumer;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['fname']." ".$row['lname'];
	}else{
		error_handling("get_costumer_name: ".$sql,mysql_error());
	}
}
/////////////////////////////////////////////////////////

function get_product_name($id_product){
	$sql = "select name from products where id_product=".$id_product;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['name'];
	}else{
		error_handling("get_product_name: ".$sql,mysql_error());
	}
}
////////////////////////////////////////////
function pre_date($dias){
	$meses =0;
	while($dias >= 30){
		$meses ++;
		$dias = $dias - 30;
	}
	
	$dia  = date('d');
	$mes  = date('m');
	$anno = date('Y');	
	
	if($dia > $dias){
		$dia = $dia - $dias;
	}else{
		$mes = $mes - 1;
		$dia = 30 + ($dia - $dias );
	}
	
	$mes = $mes - $meses;
	if($mes <=0){
		$anno = $anno - 1;
		$mes = 12 + $mes;
	}
	return $anno."-".$mes."-".$dia;
}
//////////////////////////////////////////////
  function add_roles($roles_array,$id_user){
	$sql="delete from user_rol where id_user=".$id_user;
	$result = mysql_query($sql);
	if(!$result){
		error_handling($sql,mysql_error());
	}
	for($i=0;$i < count($roles_array);$i++){
		$sql="INSERT INTO user_rol (id_rol, id_user) VALUES (".$roles_array[$i].", ".$id_user.")";
		$result = mysql_query($sql);
		if(!$result){
			
			error_handling($sql,mysql_error());
		}		
	}
  }

//////////////////////////////////////////////////////////////////////////////////////////American Credit//////////////////
function get_affiliate_name($id){
	if($id == 0){
		return "Main Account";
	}
	$sql="select name from affiliates where id_affiliate =".$id;		
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['name'];
	}else{
		error_handling("get_affiliate: ".$sql,mysql_error());
	}		
}

//////////////////////////////////////////////////////////////////////////////////////////American Credit//////////////////
function get_affiliate_id($id_order){

	$sql="select id_affiliate from orders where id_order =".$id_order;		
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['id_affiliate'];
	}else{
		error_handling("get_affiliate_id: ".$sql,mysql_error());
	}		
}
/////////////////////////////////////////////////
function get_order_status_text($id_status){
		$sql= "select name from status_list where id_status =".$id_status;
		$result = mysql_query($sql);
		if($result){
				$row = mysql_fetch_array($result);
				return $row['name'];
		}else{
			error_handling("get_order_status_text: ".$sql,mysql_error());
		}	
}
/////////////////////////////////////////////////
function get_affiliate($id_user){
	$sql= "select id_affiliate from user_affiliate where id_user =".$id_user;
	$result = mysql_query($sql);
	if($result){
		while($row = mysql_fetch_array($result)){
			$aff_list .=$row['id_affiliate'].",";				
		}
		$aff_list = substr($aff_list ,0,(strlen($aff_list)-1));
		$aff_array = explode(",",$aff_list);
		return $aff_array;
	}else{
		error_handling("get_affiliate: ".$sql,mysql_error());
	}	
}
/////////////////////////////////////////////////////////
function get_last($table,$key,$where){
	$sql ="select max(".$key.") as current from ".$table." where 1 ";
	if($where!=''){
		$sql .= " and ".$where;
	}
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['current'];
	}else{
		error_handling("get_last: ".$sql,mysql_error());
	}
}
/////////////////////////////////////////////////////////
function get_presentation_cost($id_presentation){
	$sql = "select cost from presentations where id_presentation=".$id_presentation;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);

		return $row['cost'];
	}else{
		error_handling("get_presentation_cost: ".$sql,mysql_error());
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////	
function get_affiliated_dd($id_user,$seleccionado,$extraitems,$javascript){

	if(is_admin($id_user)){
	$sql= "select af.id_affiliate, af.name 
			from  
			affiliates af order by af.name";
	
	}else{
		$sql= "select af.id_affiliate, af.name 
			from  
			affiliates af, user_affiliate ua
			where af.id_affiliate = ua.id_affiliate
			and ua.id_user= ".$id_user." order by af.name";
	
	}
		
	$result = mysql_query($sql);
	$combo= " <select name='id_affiliate' ".$javascript." >";
	if($result){
		if(mysql_num_rows($result)>0){
			$data = $extraitems;
			$num_results = mysql_num_rows($result);
			  for ($i=0; $i <$num_results; $i++){		 
				$row = mysql_fetch_array($result);
				$selected="";
				$selected = getStatus($row["id_affiliate"],$seleccionado);										
				$data.= " <option value='".$row["id_affiliate"]."'".$selected." >".$row["name"]."</option>";
			  }//for
		}else{
			$data.= " <option value='0'>Not Available</option>";
		}
	}//if
	else{
		error_handling("get_affiliated_dd: ".$sql,mysql_error());
	}
	$combo.= $data."</select>";

	return $combo;
}//function 

function get_costumer_count(){

	if($_SESSION['session_is_admin']){
		$sql = "select count(id_costumer) as cantidad from costumer ";	
	}else{
		$sql = "select count(id_costumer) as cantidad from costumer where id_affiliate in (".$_SESSION['affiliate_in'].")";
	}
	
	//echo $sql;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['cantidad'];
	}else{
		error_handling("get_costumer_count: ".$sql,mysql_error());
	}
}


function get_status_checks($seleccionados){

	$sql ="select * from status_list where name <> '' order by name asc";
	$result = mysql_query($sql);
	if($result){
		$cols = 4;
		while($row = mysql_fetch_array($result)){
			if($counter ==0){
				$data.="<tr>";
			}
			$data.="<td align='left'><input type='checkbox' name='id_status[]' value='".$row['id_status']."' ".is_in_list_v2($seleccionados,$row['id_status']).">".$row['name']."</td>";
			$counter++;
			if($counter ==$cols){
				$data.="</tr>";
				$counter =0;
			}
		}//while
		return "<table border='0' align='center'  width='100%' class='table_border'>".$data."</table>";
	}else{
		error_handling("get_status_checks: ".$sql,mysql_error());
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
function is_in_list_v2($list,$key){
	if(!is_array($list)){
		$list_array = explode(",",$list);
	}else{
		$list_array = $list;
	}
	for($i=0; $i < count($list_array);$i++){
		if($list_array[$i] == $key){
			return " checked ";
		}
	}
	return "";
}


	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
function set_affiliate($id_user,$id_affiliate){
		
	$sql ="delete from user_affiliate where id_user=".$id_user;
	$result = mysql_query($sql);
	if(!$result){
		error_handling($sql,mysql_error());
	}else{
		if(is_array($id_affiliate)){
			for($i=0; $i < count($id_affiliate); $i++){
				if($id_affiliate[$i]!=''){
					add_relation($id_affiliate[$i],$id_user);			
				}
			}
		}else{
			if($id_affiliate!=''){
				add_relation($id_affiliate,$id_user);
			}	
		}
	}	
}

function add_relation($id_affiliate,$id_user){

	$sql ="select count(id_affiliate) as existe from user_affiliate where id_affiliate=".$id_affiliate." and id_user=".$id_user;
	$result = mysql_query($sql);
	if($result){
		$row = $row = mysql_fetch_array($result);
		if($row['existe'] > 0){
			////		
		}else{
			$sql = "INSErT INTO user_affiliate (id_affiliate,id_user) VALUES (".$id_affiliate.",".$id_user.")";
			$result = mysql_query($sql);
			if(!$result){
				error_handling($sql,mysql_error());
			}			
		}
	}else{
		error_handling("add_relation: ".$sql,mysql_error());
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function get_id_parent($id_affiliate){
	if($id_affiliate!=''){
		$sql = "select id_parent from affiliates where id_affiliate=".$id_affiliate;
		$result = mysql_query($sql);
		if($result){
			$row = mysql_fetch_array($result);
			return $row['id_parent'];	
		}else{
			error_handling($sql,mysql_error());
		}	
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function get_created_by($id_affiliate){

	$sql = "select created_by from affiliates where id_affiliate=".$id_affiliate;
	$result = mysql_query($sql);
	if($result){
		$row = mysql_fetch_array($result);
		return $row['created_by'];	
	}else{
		error_handling($sql,mysql_error());
	}	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function get_notes($key , $tipo){
		if($tipo=='C'){
			$campo = "id_costumer";
		}else{
			$campo = "id_order";		
		}
		if($key!=''){
			$sql = "select * from orders_notes where ntype='".$tipo."' and ".$campo."=".$key;
			$result = mysql_query($sql);
				
			if($result){
					
				$data ="<table width='100%'>";
					
				if(mysql_num_rows($result) > 0){
	
						$data .="<tr>
							<td align='left' class='table_header' >
								Note
							</td>
							<td align='left' class='table_header' >
								Date
							</td>	
							<td align='left'class='table_header' >
								User
							</td>							
							</tr>";
												
					while($row = mysql_fetch_array($result)){
						if($_SESSION['session_is_admin'] || $_SESSION['session_is_house_agent']){
							if($tipo=='B')
								$ls ="<a href='/notes.php?id_order=".$row['id_order']."'>";
							else
								$ls ="<a href='/notes.php?id_costumer=".$row['id_costumer']."'>";
							$le ="</a>";
						}
						$data .="<tr>
							<td align='left' >
								".$ls.$row['id_order'].$le."
							</td>
							<td align='left' >
								".format_date($row['system_date'])."
							</td>	
							<td align='left' >
								".get_user_name($row['id_user'])."
							</td>							
							</tr>";
						
					}//while
					
				}//if
					
				else{
						
					$data .= "<tr><td align='center'>No notes found</td></tr>";			
					
				}
					
				$data .="</table>";
				
			}else{
					
				error_handling($sql,mysql_error());
				
			}			
			return  $data;		
		}
	}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////



function get_notes_count($id_order){
	if($id_order !=''){
		$sql ="select count(id_note) as cantidad from orders_notes where id_order=".$id_order;
		$result = mysql_query($sql);
		if($result){
			$row = mysql_fetch_array($result);
			return $row['cantidad'];
		}else{
			error_handling("get_notes_count: ".$sql,mysql_error());
		}
	}	
}

function get_consumer_notes_count($id_costumer){
	if($id_costumer !=''){
		$sql ="select count(id_costumer) as cantidad from consumer_notes where id_costumer=".$id_costumer;
		$result = mysql_query($sql);
		if($result){
			$row = mysql_fetch_array($result);
			if($row['cantidad'] < 1){
				$cantidad = 0;
			}else{
				$cantidad = $row['cantidad'];
			}
			return $cantidad ;
		}else{
			error_handling("get_consumer_notes_count: ".$sql,mysql_error());
		}
	}	
}


function get_orders_count($id_costumer){
	if($id_costumer !=''){
		$sql ="select count(id_order) as cantidad from orders where id_costumer=".$id_costumer;
		$result = mysql_query($sql);
		if($result){
			$row = mysql_fetch_array($result);
			if($row['cantidad'] < 1){
				$cantidad = 0;
			}else{
				$cantidad = $row['cantidad'];
			}
			return $cantidad ;
		}else{
			error_handling("get_consumer_notes_count: ".$sql,mysql_error());
		}
	}	
}

////////////////////////////////////////////////////////////////////////////////////

function get_orders_list($id_costumer){
	if($id_costumer !=''){
		$sql ="select o.id_order, sl.name , confirm_ip
				from orders o, status_list sl
				where 
				o.id_costumer = ".$id_costumer." and
				o.id_status = sl.id_status";
		$result = mysql_query($sql);
		if($result){
			while ($row = mysql_fetch_array($result)){
				if($row['confirm_ip']!=''){
					$confirm ="Y";
				}else{
					$confirm ="N";
				}
				$alt .="Order: ".$row['id_order']." \n Status: ".$row['name']." \nConfirm: ".$confirm;
			}
			
			return $alt ;
		}else{
			error_handling("get_consumer_notes_count: ".$sql,mysql_error());
		}
	}	
}

?>